extends ../layout

append css

block content
  .content
    .page-header
      h2=category
        | 文章列表

    .content-filter
      form.form-inline.form-filter
        .form-group
          label 分类
          select#filter-category.form-control(name="category")
            option(value="", selected=(filter.category === "")) - 选择分类 -
            each category in categories
              option(value=category._id, selected=(category._id.toString() === filter.category))= category.name

        .form-group
          label 作者
          select#filter-author.form-control(name="author")
            option(value="", selected=(filter.author === "")) - 选择作者 -
            each author in authors
              option(value=author._id, selected=(author._id.toString() === filter.author))= author.username
        .form-group
          label 关键词
          input#filter-keyword.form-control(name="keyword", type="text", value=filter.keyword)      

        button#filter-submit.btn.btn-info 筛选
        
    table.table.table-bordered
      thead
        tr
          th(width='30%')
            if(sortby == 'title')
              if(sortdir == 'desc')
                a(href='./articles?page=' + pageNum + '&sortby=title&sortdir=asc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-desc 
                  | &nbsp;标题
              else
                a(href='./articles?page=' + pageNum + '&sortby=title&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-asc 
                  | &nbsp;标题
            else
              a(href='./articles?page=' + pageNum + '&sortby=title&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-desc 
                  | &nbsp;标题
          th 
            if(sortby == 'category')
              if(sortdir == 'desc')
                a(href='./articles?page=' + pageNum + '&sortby=category&sortdir=asc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-desc 
                  | &nbsp;分类
              else
                a(href='./articles?page=' + pageNum + '&sortby=category&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-asc 
                  | &nbsp;分类
            else
              a(href='./articles?page=' + pageNum + '&sortby=category&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-desc 
                  | &nbsp;分类
          th 
            if(sortby == 'author')
              if(sortdir == 'desc')
                a(href='./articles?page=' + pageNum + '&sortby=author&sortdir=asc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-desc 
                  | &nbsp;作者
              else
                a(href='./articles?page=' + pageNum + '&sortby=author&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-asc 
                  | &nbsp;作者
            else
              a(href='./articles?page=' + pageNum + '&sortby=author&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-alpha-desc 
                  | &nbsp;作者
          th 
            if(sortby == 'published')
              if(sortdir == 'desc')
                a(href='./articles?page=' + pageNum + '&sortby=published&sortdir=asc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-amount-desc 
                  | &nbsp;发表时间
              else
                a(href='./articles?page=' + pageNum + '&sortby=published&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-amount-asc 
                  | &nbsp;发表时间
            else
              a(href='./articles?page=' + pageNum + '&sortby=published&sortdir=desc&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword)
                  i.fa.fa-sort-amount-desc 
                  | &nbsp;发表时间
          th 被赞
          th 评论
          th 状态
          th 管理
      tbody
        each article in articles
          tr
            td
              a(href='/articles/view/' + article._id, target='_blank')
                =article.title
            td
              a(href='/articles/category/' + article.category.name, target='_blank')
                =article.category.name
            td=article.author.username
            td=moment(article.created).format('YYYY-MM-DD HH:mm:ss')
            td=article.meta.favorites || 0
            td=article.comments.length || 0
            td=article.published ? '已发布' : '待发布'
            td.td-botton
              botton.btn.btn-primary(onclick="window.open('"+"/articles/view/"+article._id+"')") 查看
              botton.btn.btn-success(onclick="window.open('"+"/admin/articles/edit/"+article._id+"')") 编辑
              botton.btn.btn-danger(onclick='deleteRow("' + article._id + '")') 删除
              script(type="text/javascript").
                function deleteRow(id) {
                  Swal({
                    title: "确定删除该文章？",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: '确认删除',
                    cancelButtonText: '考虑一下',
                  }).then(function(isConfirm){
                    console.log(isConfirm);
                    if(isConfirm.value == true){
                      $.ajax({
                        url: '/admin/articles/delete/' + id,
                        type: 'POST',
                        data: {status: 'true'},
                        success: (msg) => {
                          console.log(msg.status);
                          if(msg.status){
                            Swal({
                              title: msg.text,
                              type: 'success',
                              confirmButtonText: "好的",
                            }).then(function(isConfirm){
                              if(isConfirm){
                                location.reload();
                              }
                            });
                          }else{
                            Swal({
                              title: msg.text,
                              type: 'error',
                              confirmButtonText: "好的",
                            }).then(function(isConfirm){
                              if(isConfirm){
                                location.reload();
                              }
                            });
                          }
                        },
                      })
                    }else{
                      console.log('false');
                    }
                  });
                 } 

    .paging(style='')
        nav(aria-label='Page navigation', style='text-align: center;margin:0 auto;"')
          ul.pagination
            // li
            //   a(href='#', aria-label='Previous')
            //     span(aria-hidden='true') « 上一页
            -var pagect = 1
            -var path
            while pagect <= pageCount
              -path = './articles?page=' + pagect + '&sortby='+sortby+'&sortdir='+sortdir+'&author='+filter.author+'&category='+filter.category+'&keyword='+filter.keyword
              if(pageNum == pagect)
                li.active
                  a(href=path)= pagect++
              else
                li
                  a(href=path)= pagect++
            // li
            //   a(href='#', aria-label='Next')
            //     span(aria-hidden='true') 下一页 »
            li
              span
                |文章总数：
                =dataTotal

block scriptsAfter
  script(src='/js/querystring.js')
  script(src='/js/admin/article.js')







